package com.jushu.storbox.newfragment;

import java.util.Stack;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jushu.storbox.MainFrameActivity;
import com.jushu.storbox.actions.SettingAction;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.Arguments;
import com.jushu.storbox.util.ApplicationContext;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.modelV2.request.ClientInfoResponseV2;

public abstract class AbsFrameFragment extends Fragment {

	private MainFrameActivity mMainFrameActivity = null;
	private Stack<BaseViewHolder> uiStack = new Stack<BaseViewHolder>();
	private Arguments argument = null;
	private View title;

	protected abstract int getTitleResourceId();

	protected abstract int getContentResourceId();

	protected abstract void startInitConpentData();

	protected final MainFrameActivity getMainFrameActivity() {
		if (mMainFrameActivity == null) {
			mMainFrameActivity = (MainFrameActivity) getActivity();
		}
		return mMainFrameActivity;
	}

	@Override
	public final void onAttach(Activity activity) {
		mMainFrameActivity = (MainFrameActivity) activity;
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// inflater titleView
		inflaterTitleView(inflater, container, getTitleResourceId());
		// return file layout view
		View contentView = inflaterContentView(inflater, container, getContentResourceId());
		// inflater other view
		onInflaterOtherView(inflater, container);
		return contentView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		startInitConpentData();
	}

	@Override
	public void onResume() {
		ApplicationContext instance = ApplicationContext.getInstance();
		if(instance.isFirstEntryThisTime()) {
			LogUtil.i("AbsFrameFragment", "check version change!");
			instance.setFirstEntryThisTime(false);
			msgHandler.postDelayed(new Runnable(){
				@Override
				public void run() {
					new SettingAction().getVersionInfo(getActivity(), msgHandler);
				}
			}, 2000);

		}
		super.onResume();
	}

	private void inflaterTitleView(LayoutInflater inflater, ViewGroup container, int titleResId) {
		title = inflater.inflate(titleResId, container, false);
		onLoadTitleView(title);
		setTitleView();
	}

	private View inflaterContentView(LayoutInflater inflater, ViewGroup container, int contentResId) {
		View contentView = inflater.inflate(contentResId, container, false);
		onLoadContentView(contentView);
		return contentView;
	}

	public final void setTitleView() {
		getMainFrameActivity().setTitleView(title);
	}

	protected final View getTitleView() {
		return getMainFrameActivity().getTitleView();
	}

	protected final void setActionView(View actionView) {
		getMainFrameActivity().setActionView(actionView);
	}

	protected final View getActionView() {
		return getMainFrameActivity().getActionView();
	}

	protected final void pushStack(BaseViewHolder saveUnit) {
		uiStack.push(saveUnit);
	}

	protected final BaseViewHolder pushStack() {
		return uiStack.pop();
	}

	public final void setArgument(Arguments bundle) {
		argument = bundle;
	}

	protected Arguments getArgumentBundle() {
		return argument;
	}

	/**
	 * @return true if current opend folder is root folder, other false
	 */
	protected boolean touchRoot() {
		return true;
	}

	public void doUpdateCurrentFolder(String ownerId, String folderId, String folderName) {
	}

	public void updateCurrentFolder(String ownerId, String folderId, String folderName) {
		if (isResumed()) {
			doUpdateCurrentFolder(ownerId, folderId, folderName);
		}
	}

	public void updateSelectAll(boolean selectState) {
	}

	protected class BaseViewHolder {
	}

	protected void onLoadTitleView(View titleView) {
	}

	protected void onLoadContentView(View contentView) {
	}

	/**
	 * this mothed will call when the value returned by
	 * {@link AbsFrameFragment#touchRoot()} is ture</br>
	 */
	protected void onBackPressed() {
	}

	protected void onInflaterOtherView(LayoutInflater inflater, ViewGroup container) {
	}

	/**
	 * handle the backpress event</br> relateshion mothed,such as:</br> 1.
	 * {@link AbsFrameFragment#touchRoot()}</br> 2.
	 * {@link AbsFrameFragment#onBackPressed()}</br>
	 * 
	 * @return true: if the event has handle in fragment, others false
	 */
	public final boolean handleBackPressed() {
		if (!touchRoot()) {
			onBackPressed();
			return true;
		}
		return false;
	}

	private ClientExceptionRelateHandler msgHandler = new ClientExceptionRelateHandler(){
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MessageCode.VERSION_INFO_SUCCESS:
				ClientInfoResponseV2 versionInfo = (ClientInfoResponseV2)msg.obj;
				new SettingAction().upgradeCloudDrive(getActivity(), null, versionInfo, true);
				break;
			default:
				break;
			}
		}
	
		@Override
		public Context getContext() {
			return getActivity();
		}
	};
}
